import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    flag: true,
    shoppData:[]
  },
  mutations: {
    setFlag(state, i) {
      state.flag = i
    },
    addShoppFn(state,data){
      data.flag = true
      let res = state.shoppData.findIndex(i=>i.id==data.id)
      if(res == -1){
        data.num = 1
        state.shoppData.push(data)
      }else{
        state.shoppData[res].num += 1
      }
      console.log(res);
      console.log(data);
      console.log(state.shoppData);
    }
  },
  actions: {
    layzFlag({commit},i){
      setTimeout(function(){
        commit('setFlag',i)
      },3000)
    }
  },
  modules: {},
  plugins: [createPersistedState({
    storage: localStorage,
    key: "store"
  })]
})